<template>
	<Modal
        v-model="modal"
        title="图片截取"
        >
		<div class="wrapper">
		    <div class="content">
		        <div class="cropperContent">
		          <vueCropper
		            ref="cropper"
		            :img="img"
		            :outputSize="option.size"
		            :outputType="option.outputType"
		            :info="option.info"
		            :canScale="option.canScale"
		            :autoCrop="option.autoCrop"
		            :autoCropWidth="option.autoCropWidth"
		            :autoCropHeight="option.autoCropHeight"
		            :fixed="option.fixed"
		            :fixedNumber="option.fixedNumber"
		          ></vueCropper>
		        </div>
		        <label class="btn" for="upload">上传</label>
		        <input type="file" id="upload" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event,2)">
		    </div>
		</div>
		<div slot="footer">
            <Button @click="ok" type="primary" :loading="loading">裁剪并上传</Button>
        </div>
    </Modal>
</template>
<script>
import {Modal,Button} from 'view-design';

import vueCropper from 'vue-cropper'
export default {
	props:{
		uploadUrl:{
			type : String,
			default : '',
		},
		fileArea :{
			type : String,
			default : '',
		},
		callback:{
			type : Function,
			require : true,
		}
	},
	components: {
		Modal,
		vueCropper,
		Button,
	},
	data: function () {
	    return {
	    	img : '',
	        modal: false,
	        loading : false,
	        option: {
	          	info: true,
	          	size: 1,
	          	outputType: 'jpeg',
	          	canScale: true,
	          	autoCrop: true,
	          	fixedBox: true,
	          	autoCropWidth: 300,
	          	autoCropHeight: 300,
	          	fixed: true,
	          	infoTrue: false,
	          	fixedNumber: [1, 1]
	        },
	    }
	},
	methods: {
		ok(){
			if(this.img==''){
				this.$Message.error('请上传图片');
				return false;
			}
			this.loading = true;
			this.$refs.cropper.getCropData((data) => {
	        	this.Axios.Common.AdminInfo.AvatarUpdate({pic:data,fileArea:this.fileArea})
	            .then(res =>{
	            	if(!res) return false;
	            	this.loading = false;
	            	this.modal = false;
	            	this.callback(res.data.pic);
	            })
	        })
		},
		cancel(){

		},
	    //上传图片
	    uploadImg (e, num) {
	        this.img = ''
	        var file = e.target.files[0]
	        if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
	        	this.$Message.error('图片类型必须是.gif,jpeg,jpg,png中的一种');return false;
	        }
	        var reader = new FileReader()
	        reader.onload = (e) => {
	          let data
	          data = e.target.result
	          if (typeof e.target.result === 'object') {
	            data = window.URL.createObjectURL(new Blob([e.target.result]))
	          } else {
	            data = e.target.result
	          }
	          if (num === 1) {
	            this.img = data
	          } else if (num === 2) {
	            this.img = data
	          }
	        }
	        // 转化为base64
	        reader.readAsDataURL(file);
	    },
	},
}
</script>

<style>
  * {
    margin: 0;
    padding: 0;
  }
 
  .content {
    margin: auto;
    max-width:500px;
  }
 
  .test-button {
    display: flex;
    flex-wrap: wrap;
  }
 
  .btn {
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    background: #fff;
    border: 1px solid #c0ccda;
    color: #1f2d3d;
    text-align: center;
    box-sizing: border-box;
    outline: none;
    margin:20px 10px 0px 0px;
    padding: 9px 15px;
    font-size: 14px;
    border-radius: 4px;
    color: #fff;
    background-color: #50bfff;
    border-color: #50bfff;
    transition: all .2s ease;
    text-decoration: none;
    user-select: none;
  }
 
  .des {
    line-height: 30px;
  }
 
  code.language-html {
    padding: 10px 20px;
    margin: 10px 0px;
    display: block;
    background-color: #333;
    color: #fff;
    overflow-x: auto;
    font-family: Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono;
    border-radius: 5px;
    white-space: pre;
  }
 
  .title {
    display: block;
    text-decoration: none;
    text-align: center;
    line-height: 1.5;
    margin: 20px 0px;
    background-image: -webkit-linear-gradient(left,#3498db,#f47920 10%,#d71345 20%,#f7acbc 30%,#ffd400 40%,#3498db 50%,#f47920 60%,#d71345 70%,#f7acbc 80%,#ffd400 90%,#3498db);
    color: transparent;
    -webkit-background-clip: text;
    background-size: 200% 100%;
    animation: slide 5s infinite linear;
    font-size: 40px;
  }
 
  .cropperContent {
    height: 300px;
  }
 
  .model {
    position: fixed;
    z-index: 10;
    width: 100vw;
    height: 100vh;
    overflow: auto;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
  }
 
  .model-show {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100vw;
    height: 100vh;
  }
 
  .model img {
    display: block;
    margin: auto;
    max-width: 80%;
    user-select: none;
    background-position: 0px 0px, 10px 10px;
    background-size: 20px 20px;
    background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
  }
 
  .c-item {
    display: block;
    padding: 10px 0;
    user-select: none;
  }
 
  @keyframes slide {
    0%  {
      background-position: 0 0;
    }
    100% {
      background-position: -100% 0;
    }
  }
 
  @media screen and (max-width: 1000px) {
    .content {
      max-width: 90%;
      margin: auto;
    }
 
    .test {
      height: 400px;
    }
  }
 
</style>